<?php
/**
 * Handler File Class
 *
 * @author admin <admin@wolive.cc>
 * @email admin@wolive.cc
 * @date 2017/06/01
 */

namespace app\admin\controller;

use think\Controller;
use think\Loader;
use think\Response;
use think\Session;
use app\admin\model\Admins;
use app\extra\push\Pusher;

/**
 * 基础验证是否登录.
 */
class Base extends Controller
{

    /**
     * 验证session.
     *
     * @return void
     */
    protected function _initialize()
    {
        if (empty($_SESSION['Msg']['service_id'])) {
            $this->redirect('admin/login/index');
        }

        $login = $_SESSION['Msg'];
        $res =Admins::table('wolive_business')->where('business_id',$login['business_id'])->find();

        $group =Admins::table('wolive_group')->where('business_id',$login['business_id'])->select();

        $groupjson =json_encode($group);

        $data = json_encode($login);
        $app_key = app_key;

        $arr = parse_url(whost);

        if ($arr['scheme'] == 'ws') {
            $value = 'false';
            $port = 'wsPort';
        } else {
            $value = 'true';
            $port = 'wssPort';
        }

        // 如果开启了检查过期设置则检查账号是否过期
        $expire = '';
        $exclude = ['admin', 'test', 'wolive'];
        if (!in_array($login['business_id'], $exclude)) {
            $time = time();
            $diff = $time - $res['exp_timestamp'];
            if ($diff > 0) {
                $expire = '<h3 style="color:red;display:inline;line-height:230%;margin-left: 30px;">该试用账号已经到期，请到<a href="https://wolive.cc" style="color:darkred;">wolive官网</a>联系客服购买授权</h3>';
            } else if ($diff > -3*24*60*60) {
                $expire = '<h3 style="color:red;display:inline;line-height:230%;margin-left: 30px;">该试用账号即将过期，请到<a href="https://wolive.cc" style="color:darkred;">wolive官网</a>联系客服购买授权</h3>';
            }
        }

        $this->assign('expire', $expire);
        $this->assign('app_key', $app_key);
        $this->assign('whost',$arr['host']);
        $this->assign('value', $value);
        $this->assign('wport', wport);
        $this->assign('arr', $login);
        $this->assign('data', $data);
        $this->assign('port', $port);
        $this->assign('group',$groupjson);
        $this->assign('voice',$res['voice_state']);
        $this->assign('voice_address',$res['voice_address']);

    }

    public static function chatDecode($content) {
        // 表情替换
        $content = preg_replace_callback('/face\[(.*?)\]/', function ($matches) {
            $emo_map = ['互粉' => 'emo_11','亲亲' => 'emo_27','伤心' => 'emo_60','偷笑' => 'emo_26','兔子' => 'emo_08','可怜' => 'emo_17','可爱' => 'emo_16','右哼哼' => 'emo_31','吃惊' => 'emo_19','呕吐' => 'emo_36','哈哈' => 'emo_15','嘘' => 'emo_33','嘻嘻' => 'emo_14','囧' => 'emo_10','围观' => 'emo_05','太开心' => 'emo_29','失望' => 'emo_48','奥特曼' => 'emo_09','好色' => 'emo_50','委屈' => 'emo_35','威武' => 'emo_06','害羞' => 'emo_20','左哼哼' => 'emo_32','微笑' => 'emo_13','心' => 'emo_59','怒' => 'emo_39','怒骂' => 'emo_58','思考' => 'emo_43','悲伤' => 'emo_54','打呼' => 'emo_45','打哈欠' => 'emo_37','抓狂' => 'emo_55','抠鼻' => 'emo_18','抱抱' => 'emo_38','拜拜' => 'emo_42','晕' => 'emo_53','汗' => 'emo_44','流泪' => 'emo_25','浮云' => 'emo_03','熊猫' => 'emo_07','爱你' => 'emo_24','生气' => 'emo_51','生病' => 'emo_28','白眼' => 'emo_30','睡' => 'emo_46','礼物' => 'emo_12','神马' => 'emo_02','给力' => 'emo_04','羊驼' => 'emo_01','衰' => 'emo_34','调皮' => 'emo_21','鄙视' => 'emo_23','酷' => 'emo_49','钱' => 'emo_47','闭嘴' => 'emo_22','问号' => 'emo_40','阴险' => 'emo_57','馋' => 'emo_41','黑线' => 'emo_56','鼓掌' => 'emo_52'];
            return isset($emo_map[$matches[1]]) ? '<img src="/upload/emoji/'.$emo_map[$matches[1]].'.gif">' : $matches[0];
        }, $content);

        $content_old = $content;

        // 图片替换
        $content = preg_replace('/\!\[.*?\]\((.*?)\)/', '<img src="$1" onclick="getbig(this)" >', $content);

        // 文件替换
        $content = preg_replace('/file\[(.*?)\]\((.*?)\)/', '<div><a href="$2" style=\'display: inline-block;text-align: center;min-width: 70px;text-decoration: none;\' download=\'$1\'><i class=\'layui-icon\' style=\'font-size: 60px;\'>&#xe61e;</i><br>$1</a></div>', $content);

        // 语音替换
        $content = preg_replace('/voice\((.*?)\)/', '<div style="cursor:pointer;text-align:center;" onclick="getstate(this)" data="play"><audio src="$1"></audio><i class="layui-icon" style="font-size:25px;">&#xe652;</i><p>音频消息</p></div>', $content);

        // 不是语音、文件、图片类型的数据则识别url
        if ($content == $content_old) {
            // url识别
            $preg = '@(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))@';
            $content = preg_replace($preg, '<a href="$1" target="_blank">$1</a>', $content);
        }

        return $content;
    }

    /**
     * 获得推送接口实例
     *
     * @return Pusher
     */
    public static function getPusher()
    {
        $sarr = parse_url(ahost);
        if ($sarr['scheme'] == 'https') {
            $state = true;
        } else {
            $state = false;
        }

        $app_key = app_key;
        $app_secret = app_secret;
        $app_id = app_id;
        $options = array(
            'encrypted' => $state
        );
        $host = ahost;
        $port = aport;

        $pusher = new Pusher(
            $app_key,
            $app_secret,
            $app_id,
            $options,
            $host,
            $port
        );
        return $pusher;
    }


}
